<?php

namespace Core\MVC;

class RedirectViewHandler implements IViewHandler {
    
    /**
     * @var type \Core\MVC\IViewHandler
     */
    private $adaptedViewHandler;
    
    /**
     * @var \Core\MVC\IRouter 
     */
    private $router;
    private $prefix;
    
    public function render($modelMap) {
        if(substr($modelMap->getViewName(), 0, strlen($this->prefix)) == $this->prefix) {
            $location = $this->router->getActionUrl(substr($modelMap->getViewName(), strlen($this->prefix)));
            header("Location: $location");
        } else {
            $this->adaptedViewHandler->render($modelMap);
        }
    }   
    
    public function setAdaptedViewHandler($adaptedViewHandler) {
        $this->adaptedViewHandler = $adaptedViewHandler;
    }
    
    public function setRouter($router) {
        $this->router = $router;
    }
    
    public function setPrefix($prefix) {
        $this->prefix = $prefix;
    }
}